﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.BLL;
using System.Web.UI.HtmlControls;
using GCELibrary.Current;
using ZF.Model;
using System.Transactions;
using ZF.BLL.Page;

namespace ZF.WS.info
{
    public partial class pub : LMPage
    {/// <summary>
        /// 报告生成器
        /// </summary>
        public override WarnFactory WarnFactory
        {
            get
            {
                return new WarnFactory("/warn.aspx");

            }
        }

        /// <summary>
        /// 当前页面的操作行为
        /// </summary>
        [PageArgument(Key = "ot", DefaultValue = PageOperType.AddNew)]
        public override PageOperType OperType { get; set; }
        [PageArgument(Key = "id", CanNull=false, BindOperType=PageOperType.Edit)]
        public int Id { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (this.OperType == PageOperType.AddNew)
                {
                    Add();
                }
                else if (this.OperType == PageOperType.Edit)
                {
                    Edit();
                }
            }
        }
        public void InitControls()
        {
            this.ddlType.DataSource = DictionaryConfig.IInfoType.Select(p =>new{
                     Key=p.InfoTypeId,
                     Text=p.Name,
                }).ToList();
            this.ddlType.DataBind();
        }


        public void Add()
        {
            InitControls();
            var member = SiteInfo.GetCurrentMember();
            if (member != null)
            {
                this.tbContact.Text = member.FullName;
                this.tbEmail.Text = member.EMail;
                this.tbQQ.Text = member.QQ;
                this.tbPhone.Text = member.Phone;
                this.tbAddress.Text = member.Address;
            }

        }
        public void AddSubmit(object sender, EventArgs e)
        {

            using (var ts = new TransactionScope())
            {
                using (var dc = SiteInfo.CreateDBDataContext())
                {
                    var info = new Model.I_Info()
                    {
                        Address = this.tbAddress.Text,
                        AreaId1 = this.tbCity.Text.Convert<int>(),
                        AddTime = DateTime.Now,
                        AreaId2 = this.tbArea.Text.EmptyInt(),
                        Contact = this.tbContact.Text,
                        Email = this.tbEmail.Text,
                        IsCompany = this.ddlCompany.SelectedValue == "1",
                        Photo = this.SWFUpLoad1.UpLoadFileInfos.ConstituteString(";"),
                        Phone = this.tbPhone.Text,
                        Price = this.tbPrice.Text.Convert<int>(0),
                        QQ = this.tbQQ.Text,
                        Title = this.tbTitle.Text,
                        IsTop = false,
                        Status = 1,
                        Other = null,
                        IsVerify = SiteInfo.SystemConfig.HousePubVerify,
                        InfoTypeId = this.ddlType.SelectedValue.Convert<int>(),
                        Content = this.tbContent.Text,
                        Tag=this.tbTag.Text,
                        MemberId = SiteInfo.GetCurrentMember() == null ? 0 : SiteInfo.GetCurrentMember().MemberId,
                        Period = this.ddlPeriod.SelectedValue,
                    };
                    SiteInfo.CheckFilterWords(info);
                    try
                    {
                        dc.I_Info.InsertOnSubmit(info);
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
 

                    WarnFactory.CreateWarn(new PageWarnParameter()
                    {
                        JscriptMode = true,
                        Link = WarnLink.Current,
                        Message = "发布成功",
                        DoPage = string.Format("/info/view.aspx?id={0}", info.InfoId),
                    }).Alert();
                    return;


                    string msgKey = "信息";
                    SiteInfo.InsertLog("发布" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功发布一个{1}{0}，点击确定按钮返回上一页", msgKey, this.ddlType.SelectedItem.Text));
                    link.AddWarnMessageLink(string.Format("查看该{0}", msgKey),
                        string.Format("~/info/view.aspx?id={0}", info.InfoId));
                    link.AddWarnMessageLink(string.Format("继续发布{0}", msgKey),
                        Request.Url.ToString());
                    var fromUrl = this.ViewState["fromUrl"];
                    if (fromUrl != null)
                    {
                        WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, fromUrl.ToString()).Alert();
                    }
                    else
                    {
                        WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, WarnLink.GoBack).Alert();
                    }

                }

            }


        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                var member = SiteInfo.GetCurrentMember();
                if (member == null)
                    throw WarnFactory.CreateWarn(WarnCode.LoginTimeOut);
                I_Info info = null;
                if(member.UserType== UserType.Member)
                  info = dc.I_Info.FirstOrDefault(p => p.InfoId == this.Id && p.MemberId==member.MemberId );
                else
                    info = dc.I_Info.FirstOrDefault(p => p.InfoId == this.Id );
                if (info == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
 
                InitControls();  
                 this.tbEmail.Text=info.Email;
                 this.ddlCompany.SelectedValue = info.IsCompany ? "1" : "0";
                this.tbPrice.Text= info.Price.ToString();
                this.tbTitle.Text=info.Title ;
                this.ddlType.SelectedValue= info.InfoTypeId.ToString();
                this.tbCity.Text = info.AreaId1.ToString();
                this.tbArea.Text = info.AreaId2.ToString();
                this.tbAddress.Text = info.Address;
                this.tbContact.Text = info.Contact;
                this.tbQQ.Text = info.QQ;
                this.tbTitle.Text = info.Title;
                this.tbPhone.Text = info.Phone;
                this.ddlPeriod.SelectedValue = info.Period;
                this.tbPhoto.Text = info.Photo;
                this.tbContent.Text = info.Content;
                this.tbTag.Text = info.Tag;

                this.lvImgList.DataSource = info.Photo.EmptyString("").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(p => new
                    {
                        File = p
                    });
                this.lvImgList.DataBind();

            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var member = SiteInfo.GetCurrentMember();
                    if (member == null)
                        throw WarnFactory.CreateWarn(WarnCode.LoginTimeOut);
                    I_Info info = null;
                    if (member.UserType == UserType.Member)
                        info = dc.I_Info.FirstOrDefault(p => p.InfoId == this.Id && p.MemberId == member.MemberId);
                    else
                        info = dc.I_Info.FirstOrDefault(p => p.InfoId == this.Id);
                    if (info == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                    info.AreaId1 = this.tbCity.Text.Convert<int>();
                    info.AreaId2 = this.tbArea.Text.EmptyInt();
                    info.Address = this.tbAddress.Text;
                    info.Contact = this.tbContact.Text; 
                    info.QQ = this.tbQQ.Text; 
                    info.Title = this.tbTitle.Text;
                    info.Phone = this.tbPhone.Text;
                    info.Period = this.ddlPeriod.SelectedValue;
                    info.Email = this.tbEmail.Text;
                    info.Content = this.tbContent.Text;
                    var photoList = this.tbPhoto.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    photoList.AddRange(this.SWFUpLoad1.UpLoadFileInfos);
                    info.Photo = photoList.ConstituteString(";"); ;
                    info.InfoTypeId = this.ddlType.SelectedValue.Convert<int>();
                    info.IsCompany = this.ddlCompany.SelectedValue == "1";
                    info.Tag = this.tbTag.Text;

                    SiteInfo.CheckFilterWords(info);

                    try
                    {
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }

                    WarnFactory.CreateWarn(new PageWarnParameter()
                    {
                        JscriptMode = true,
                        Link = WarnLink.Close,
                        Message = "修改成功",

                    }).Alert();
                    return;

                    string msgKey = "信息";

                    SiteInfo.InsertLog("修改" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功修改一个{1}{0}，点击确定按钮返回{0}列表", msgKey, this.ddlType.SelectedItem.Text));
                    link.AddWarnMessageLink(string.Format("查看该{0}", msgKey),
                        string.Format("/info/view.aspx?id={0}", info.InfoId));
                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey),
                        Request.Url.ToString());
                    var fromUrl = this.ViewState["fromUrl"];

                    if (fromUrl != null)
                    {
                        WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, fromUrl.ToString()).Alert();
                    }
                    else
                    {
                        WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, WarnLink.GoBack).Alert();
                    }









                }


            }

        }

    }
}
